package demo3;


    //  拷贝 自定义类型

class Money implements Cloneable{
    public  double money = 12.25;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

class Student implements Cloneable{

    public String name;
    public Money m = new Money();

    public Student(){

    }
    public Student(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }

    // object是所有类的父类
    @Override
    protected Object clone() throws CloneNotSupportedException {
        // 只是克隆了student 对象
        Student student = (Student) super.clone();
        // 克隆了 Student 对象里的 Money 对象
        student.m = (Money) this.m.clone();    // 两次拷贝（克隆）
        return student;
    }
}


public class test {

    public static void main(String[] args) throws CloneNotSupportedException{

        Student student1 = new Student();
        // 强制类型转换  向下转型 有父类强转为子类
        Student student2 = (Student) student1.clone();

        System.out.println(student1.m.money);
        System.out.println(student2.m.money);
        System.out.println("===================");
        student2.m.money = 99;

        System.out.println(student1.m.money);
        System.out.println(student2.m.money);


    }


}
